using System.ComponentModel;
using System;
using System.Diagnostics;
namespace AppliedRecognition.Speech.TaskSupport
{
	/// <summary>
	/// Summary description for WindowSupport.
	/// </summary>
	public class WindowSupport
	{
		private WindowSupport()
		{
		}

		public static short VK_MENU = 0x12;
		public static short VK_LWIN = 0x5B;
		public static short VK_DOWN = 0x28;
		public static short VK_RETURN = 0x0D;
		public static short INPUT_KEYBOARD = 0x01;
		public static int KEYEVENTF_EXTENDEDKEY =  0x0001;
		public static int KEYEVENTF_KEYUP = 0x0002;
		public static int  KEYEVENTF_SCANCODE =   0x0008;

		static internal void SendInputDownArrowEnter(uint cnt)
		{
			uint inputItems = (cnt+1)*2;
			INPUT[] inputs = new INPUT[inputItems];

			
			for (int ii=0;ii< inputItems;ii++)
			{
				inputs[ii].type = INPUT_KEYBOARD;
				inputs[ii].ki.wVk = VK_DOWN;
				if (ii % 2 == 1)
				{
					inputs[ii].ki.dwFlags =  KEYEVENTF_KEYUP;
				}
			}

			inputs[inputItems-2].ki.wVk = VK_RETURN;
			inputs[inputItems-1].ki.wVk = VK_RETURN;


			unsafe 
			{
				if (PWin32.SendInput(inputItems,inputs, sizeof(INPUT)) != inputItems)
				{
					Debug.WriteLine(W32ExMsg(""));
				}
			}

		}

		static public void SendInputWindowsD()
		{
		
			INPUT[] inp = new INPUT[4];

			inp[0].type = inp[1].type = inp[2].type = inp[3].type = INPUT_KEYBOARD;

			inp[0].ki.wVk = VK_LWIN;
			inp[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
			

			inp[1].ki.wVk = 68;
			

			inp[2].ki.wVk = 68;
			inp[2].ki.dwFlags =  KEYEVENTF_KEYUP;

			inp[3].ki.wVk = VK_LWIN;
			inp[3].ki.dwFlags = KEYEVENTF_EXTENDEDKEY| KEYEVENTF_KEYUP;
			
			unsafe 
			{
				if (PWin32.SendInput(4,inp, sizeof(INPUT)) == 0)
				{
					Debug.WriteLine(W32ExMsg(""));
				}
			}
		}


		public static string W32ExMsg(string msg)
		{
			Win32Exception ex = new Win32Exception();
			return msg + " (" + ex.NativeErrorCode.ToString() + ") "+ ex.Message;
		}




	}
}
